package com.itcp.itelpicture.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.itcp.itelpicture.domain.dto.remark.LikedTimesDTO;
import com.itcp.itelpicture.domain.po.LikedStat;
import com.itcp.itelpicture.domain.po.Picture;
import com.itcp.itelpicture.domain.po.community.CommunityPost;
import com.itcp.itelpicture.mapper.LikedStatMapper;
import com.itcp.itelpicture.service.ICommunityPostService;
import com.itcp.itelpicture.service.ILikedStatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcp.itelpicture.service.IPictureService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 点赞统计表 服务实现类
 * </p>
 *
 * @author wgzz
 * @since 2025-04-20
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class LikedStatServiceImpl extends ServiceImpl<LikedStatMapper, LikedStat> implements ILikedStatService {

    private final ICommunityPostService communityPostService;

    private final IPictureService pictureService;

    @Override
    public void updateLikedTimes(String objType, List<LikedTimesDTO> msg) {
        // 1.更新本地数据库
        List<Long> objectIds = msg.stream().map(LikedTimesDTO::getObjectId).collect(Collectors.toList());
        // 1.1.查询旧数据
        List<LikedStat> oldStats = lambdaQuery().in(LikedStat::getObjectId, objectIds).list();
        Map<Long, LikedStat> oldStatMap = new HashMap<>(0);
        if(CollUtil.isNotEmpty(oldStats)) {
            oldStatMap = oldStats.stream().collect(Collectors.toMap(LikedStat::getObjectId, Function.identity()));
        }
        // 1.2.处理为PO
        List<LikedStat> list = new ArrayList<>(msg.size());
        for (LikedTimesDTO dto : msg) {
            // 判断objectId是否已经存在
            LikedStat stat = oldStatMap.get(dto.getObjectId());
            if(stat == null){
                // 不存在，新增
                stat = new LikedStat();
                stat.setObjectId(dto.getObjectId());
                stat.setLikedTimes(dto.getLikedTimes());
                stat.setObjType(objType);
            }else{
                // 已经存在，需要更新
                stat.setLikedTimes(dto.getLikedTimes());
            }
            list.add(stat);
        }
        // 1.3.批处理
        saveOrUpdateBatch(list);

        // 2.更新对应业务点赞ids
        updateLikeCounts(list);
    }

    /**
     * 更新文章或者图片点赞数量
     * @param list
     */
    private void updateLikeCounts(List<LikedStat> list) {
        CommunityPost post = new CommunityPost();
        Picture pic = new Picture();
        List<CommunityPost> postList = new ArrayList<>();
        List<Picture> picList = new ArrayList<>();
        for (LikedStat r : list) {
            if (r.getObjType().equals("POST")){
                post.setId(r.getObjectId());
                post.setLikeCount(r.getLikedTimes());
                postList.add(post);
            }else{
                pic.setId(r.getObjectId());
                pic.setThumbNum(r.getLikedTimes());
                picList.add(pic);
            }
        }
        if (CollUtil.isNotEmpty(postList)){
            communityPostService.updateBatchById(postList);
            log.info("批量更新文章点赞数量成功: {}", postList);
        }
        if (CollUtil.isNotEmpty(picList)){
            pictureService.updateBatchById(picList);
            log.info("批量更新图片点赞数量成功: {}", picList);
        }
    }
}
